package tongwentongshu.com.app.activity;

import android.support.v4.util.ArrayMap;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import de.greenrobot.event.EventBus;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.adapter.HelpAdapter;
import tongwentongshu.com.app.adapter.MyShareAdapter;
import tongwentongshu.com.app.bean.HelpBean;
import tongwentongshu.com.app.bean.MyReadingBean;
import tongwentongshu.com.app.bean.MyShareBean;
import tongwentongshu.com.app.bean.SumEvent;
import tongwentongshu.com.app.network.ResponseSubscriber;
import tongwentongshu.com.app.update.UpdateFractory;

/**
 * Created by XiongBin on 2017/7/24.
 */

public class OnlineHelpActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.rv_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private Map<String, String> map;
    protected boolean onRefreshJudge = false;
    private HelpAdapter helpAdapter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_my_follow;
    }

    @Override
    protected int getTitleId() {
        return R.string.help;
    }

    @Override
    protected void initView() {
        initList();
    }

    @Override
    protected void initData() {
        initMyReading();
    }

    private void initList() {
        onRefreshJudge = true;
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        List arr = new ArrayList<MyReadingBean.DataBean>();
        helpAdapter = new HelpAdapter(mContext, arr);
        helpAdapter.setOnLoadMoreListener(this, mRecyclerView);
        helpAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
//        View empty = getLayoutInflater().inflate(R.layout.empty_no_data, (ViewGroup) mRecyclerView.getParent(), false);
//        //pullToRefreshAdapter.setPreLoadNumber(3);
//        helpAdapter.setEmptyView(empty);
        mRecyclerView.setAdapter(helpAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
                Toast.makeText(mContext, Integer.toString(position), Toast.LENGTH_SHORT).show();
            }
        });
    }

    void initMyReading() {
        map = new ArrayMap<>();

        UpdateFractory.getBuild()
                .name("MyProblemHelpCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<HelpBean>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(HelpBean myshare, String msg) {
                        chengeData(myshare);
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });

    }

    @Override
    public void onRefresh() {
        onRefreshJudge = true;
//        pullToRefreshAdapter.setEnableLoadMore(false);
        initMyReading();
    }

    @Override
    public void onLoadMoreRequested() {
        helpAdapter.loadMoreEnd(true);
    }

    void chengeData(HelpBean msg) {
        if (msg.getData() != null && msg.getData().size() > 0) {
            if (onRefreshJudge) {
//                Log.e(TAG, "onRefreshJudge: " + onRefreshJudge);
                helpAdapter.setNewData(msg.getData());
                mSwipeRefreshLayout.setRefreshing(false);
                helpAdapter.setEnableLoadMore(true);
                onRefreshJudge = false;
            }
        } else {
            mSwipeRefreshLayout.setRefreshing(false);
            helpAdapter.setEnableLoadMore(true);
            onRefreshJudge = false;
        }
    }
}
